/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aybolit.core.security;

/**
 *
 * @author Masumov Emin
 */
import aybolit.core.objects.dao.OperatorsDao;
import aybolit.core.objects.model.Operators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class AuthManager implements AuthenticationManager {
//    HospitalBo hospitalsBo;
//    OperatorsBo operatorsBo;
   OperatorsDao  operatorsDao;
    private Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();

    @Override
    public Authentication authenticate(Authentication auth)
            throws AuthenticationException {
            ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpSession ses=attr.getRequest().getSession(true);
        
        Operators operlist = operatorsDao.findAlloperators(auth.getName());
        if(operlist!=null){
        if (operlist.getPassword().equals(auth.getCredentials())) {
            ses.setAttribute("oper", operlist);
            
            return new UsernamePasswordAuthenticationToken(
                    auth.getName(),
                    auth.getCredentials(),
                    getAuthorities(operlist.getOperTypeId()));
        } else {
            ses.setAttribute("error", "2");
            throw new BadCredentialsException("Wrong password!");
          
        }
        }else{
              ses.setAttribute("error", "1");
            throw new BadCredentialsException("USER NOT FOUND");
        
        }
    }

    /**
     * Retrieves the correct ROLE type depending on the access level, where
     * access level is an Integer. Basically, this interprets the access value
     * whether it's for a regular user or admin.
     *
     * @param access an integer value representing the access of the user
     * @return collection of granted authorities
     */
    public Collection<GrantedAuthority> getAuthorities(int access) {
        // Create a list of grants for this user
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);

        // All users are granted with ROLE_USER access
        // Therefore this user gets a ROLE_USER by default
        authList.add(new GrantedAuthorityImpl("ROLE_USER"));
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
        // Check if this user has admin access 
        // We interpret Integer(1) as an admin user
        if (access == 1) {
            // User has admin access
            authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
        }

        // Return list of granted authorities
        return authList;
    }

//    public OperatorsBo getOperatorsBo() {
//        return operatorsBo;
//    }
//
//    public void setOperatorsBo(OperatorsBo operatorsBo) {
//        this.operatorsBo = operatorsBo;
//    }
//
//    public HospitalBo getHospitalsBo() {
//        return hospitalsBo;
//    }
//
//    public void setHospitalsBo(HospitalBo hospitalsBo) {
//        this.hospitalsBo = hospitalsBo;
//    }

    public void setOperatorsDao(OperatorsDao operatorsDao) {
        this.operatorsDao = operatorsDao;
    }
    
    
}